use std::time::Duration;

use futures::executor::block_on;
use tokio::{join, runtime::{self, Runtime}, time::sleep};

pub fn run(){
    print_thread();
    //创建一个单线程的tokio运行时
    runtime::Builder::new_current_thread()
        .enable_all()
        .build().unwrap()
            .block_on(async{
                let f1 = println_num(12);
                let f2 = println_num2(12);
                join!(f1,f2);
            });

    println!("--------------");
    //也可以使用 futures 提供的工具来运行async 函数
    block_on(async{
        let f1 = println_num2(20);
        let f2 = println_num3(21);
        join!(f1,f2);
    })
}

async fn println_num(num: i32) {
    print_thread();
    sleep(Duration::from_secs(1)).await;
    println!("num1 = {}", num);
}
async fn println_num2(num: i32) {
    print_thread();
    println!("num2 = {}", num);
    println_num3(num).await;
}
async fn println_num3(num: i32) {
    print_thread();
    println!("num3 = {}", num);
}
fn print_thread(){
    println!("current thread id is {:?}", std::thread::current().id());
}